package com.afd.ozblip.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.CityEntity;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.entity.SignEntity;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DBHelper extends OrmLiteSqliteOpenHelper {

	// private static String TAG = DBHelper.class.getSimpleName();

	private static int VERSION = 12;

	private static String DB_NAME = "ozblip.dba";

	private Dao<Object, Integer> mDao;

	Context mContext;

	Class<?>[] tableClasss = new Class<?>[] { BusinessEntity.class,
			ReviewEntity.class, SignEntity.class,CityEntity.class };

	public DBHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {

		try {
			for (int i = 0, len = tableClasss.length; i < len; i++) {
				TableUtils.createTable(arg1, tableClasss[i]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
			int arg3) {
		try {

			for (Class<?> cls : tableClasss) {
				TableUtils.dropTable(arg1, cls, true);
			}

			onCreate(arg0, arg1);
		} catch (SQLException e) {
		}
	}

	@Override
	public void close() {
		super.close();
		mDao = null;
	}

	public Dao<Object, Integer> getHelper(Class<?> cls) {
		try {
			mDao = (Dao<Object, Integer>) getDao(cls);
			return mDao;
		} catch (SQLException e) {
			return null;
		}
	}

}
